package com.example.onenetdht11;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;

import com.example.onenetdht11.entity.User2;
import com.example.onenetdht11.util.OneNetUtils;
import com.google.gson.Gson;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Range;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.HeConfig;
import com.qweather.sdk.view.QWeather;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class MyFragment extends Fragment{
    View view;
    MainActivity ma;
    ImageButton citychange;
    TextView tvcity,tvjueche,tvweather,tvwind,tvoutsidetemp;
    TextView tv1Dtime ,tv2Dtime,tv3Dtime,tv4Dtime,tv5Dtime,tv6Dtime,tv7Dtime;
    TextView tv1Dmintemp,tv2Dmintemp,tv3Dmintemp,tv4Dmintemp,tv5Dmintemp,tv6Dmintemp,tv7Dmintemp;
    TextView tv1Dmaxtemp,tv2Dmaxtemp,tv3Dmaxtemp,tv4Dmaxtemp,tv5Dmaxtemp,tv6Dmaxtemp,tv7Dmaxtemp;
    TextView tv1Dtextday,tv2Dtextday,tv3Dtextday,tv4Dtextday,tv5Dtextday,tv6Dtextday,tv7Dtextday;
    TextView tv1Dwind,tv2Dwind,tv3Dwind,tv4Dwind,tv5Dwind,tv6Dwind,tv7Dwind;
    double[][] tempHourKmeansvalue;
    double[][] humHourKmeansValue;
    ArrayList<Double> tempHourValues;
    ArrayList<Double> humHourValues;
    double[] tempCenter;
    double[] systemTempCenter;  //系统默认的温度范围
    double[] systemHumCenter;  //系统默认的湿度范围
    double systemTempMax,systemTempMin;
    double userTempMax,userTempMin; //用户自定义的温度范围
    double systemHumMin,systemHumMax;
    String isOld,isDisease;
    User2 user;
    Intent intent;
    NotificationManager notifyManager;
    OneNetUtils oneNetUtils;
    public MyFragment(){

    }
    public MyFragment(MainActivity mainActivity,User2 user,OneNetUtils oneNetUtils) {
        this.ma = mainActivity;
        this.user = user;
        this.oneNetUtils = oneNetUtils;
    }

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);//第一个参数是布局文件名，第二个是父容器
        initView();
        initListener();
        initWeather();
        initData();
        initKmeans();
        intent = new Intent(ma,MainActivity.class);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1){
            initView();
            initListener();
            initWeather();
            initData();
            initKmeans();
        }
    }

    void initView(){
        citychange = view.findViewById(R.id.ic_city_change);
        tvcity = view.findViewById(R.id.tv_city);
        tvjueche = view.findViewById(R.id.tv_jueche);
        tvwind = view.findViewById(R.id.tv_wind);
        tvweather = view.findViewById(R.id.tv_weather);
        tvoutsidetemp = view.findViewById(R.id.tv_outsidetemp);

        tv1Dtime = view.findViewById(R.id.tv_1D_time);
        tv2Dtime = view.findViewById(R.id.tv_2D_time);
        tv3Dtime = view.findViewById(R.id.tv_3D_time);
        tv4Dtime = view.findViewById(R.id.tv_4D_time);
        tv5Dtime = view.findViewById(R.id.tv_5D_time);
        tv6Dtime = view.findViewById(R.id.tv_6D_time);
        tv7Dtime = view.findViewById(R.id.tv_7D_time);

        tv1Dmintemp = view.findViewById(R.id.tv_1D_mintemp);
        tv2Dmintemp = view.findViewById(R.id.tv_2D_mintemp);
        tv3Dmintemp = view.findViewById(R.id.tv_3D_mintemp);
        tv4Dmintemp = view.findViewById(R.id.tv_4D_mintemp);
        tv5Dmintemp = view.findViewById(R.id.tv_5D_mintemp);
        tv6Dmintemp = view.findViewById(R.id.tv_6D_mintemp);
        tv7Dmintemp = view.findViewById(R.id.tv_7D_mintemp);

        tv1Dmaxtemp = view.findViewById(R.id.tv_1D_maxtemp);
        tv2Dmaxtemp = view.findViewById(R.id.tv_2D_maxtemp);
        tv3Dmaxtemp = view.findViewById(R.id.tv_3D_maxtemp);
        tv4Dmaxtemp = view.findViewById(R.id.tv_4D_maxtemp);
        tv5Dmaxtemp = view.findViewById(R.id.tv_5D_maxtemp);
        tv6Dmaxtemp = view.findViewById(R.id.tv_6D_maxtemp);
        tv7Dmaxtemp = view.findViewById(R.id.tv_7D_maxtemp);

        tv1Dtextday = view.findViewById(R.id.tv_1D_textday);
        tv2Dtextday = view.findViewById(R.id.tv_2D_textday);
        tv3Dtextday = view.findViewById(R.id.tv_3D_textday);
        tv4Dtextday = view.findViewById(R.id.tv_4D_textday);
        tv5Dtextday = view.findViewById(R.id.tv_5D_textday);
        tv6Dtextday = view.findViewById(R.id.tv_6D_textday);
        tv7Dtextday = view.findViewById(R.id.tv_7D_textday);

        tv1Dwind = view.findViewById(R.id.tv_1D_wind);
        tv2Dwind = view.findViewById(R.id.tv_2D_wind);
        tv3Dwind = view.findViewById(R.id.tv_3D_wind);
        tv4Dwind = view.findViewById(R.id.tv_4D_wind);
        tv5Dwind = view.findViewById(R.id.tv_5D_wind);
        tv6Dwind = view.findViewById(R.id.tv_6D_wind);
        tv7Dwind = view.findViewById(R.id.tv_7D_wind);

    }
    void initWeather(){
        HeConfig.init("HE2205070945471173","abe075bcfc304e6d9132dbe358538d43");
        HeConfig.switchToDevService();
        getWeather();
    }


    private void getWeather() {
        if (user.getCounty() != null && !user.getCounty().equals("")) {
            //获取城市编码
            QWeather.getGeoCityLookup(ma, user.getCounty(), Range.CN,1, Lang.ZH_HANS, new QWeather.OnResultGeoListener() {
                @Override
                public void onError(Throwable throwable) {
                    Log.i("city", "getCity onError: ");
                }

                @Override
                public void onSuccess(GeoBean geoBean) {
                    Log.i("city", "getCity onSuccess: " + new Gson().toJson(geoBean));
                    if (Code.OK == geoBean.getCode()){
                        List<GeoBean.LocationBean> locationBeanList = geoBean.getLocationBean();
                        GeoBean.LocationBean locationBean = locationBeanList.get(0);
                        String cityId = locationBean.getId();
                        System.out.println(locationBean.getName());
                        System.out.println(cityId);
                        //获取实时天气
                        QWeather.getWeatherNow(ma,cityId,
                                new QWeather.OnResultWeatherNowListener() {
                                    @Override
                                    public void onError(Throwable e) {
                                        Log.i("weather", "getWeather onError: " + e.getLocalizedMessage());
                                    }

                                    @Override
                                    public void onSuccess(WeatherNowBean weatherBean) {
                                        Log.i("weather", "getWeather onSuccess: " + new Gson().toJson(weatherBean));
                                        //先判断返回的status是否正确，当status正确时获取数据，若status不正确，可查看status对应的Code值找到原因
                                        if (Code.OK == weatherBean.getCode()) {
                                            WeatherNowBean.NowBaseBean now = weatherBean.getNow();
                                            if (now!=null){
                                                String temp = now.getTemp();
                                                temp = temp + "℃";
                                                String weather = now.getText();
                                                String wind = now.getWindDir();
                                                tvoutsidetemp.setText(temp);
                                                tvweather.setText(weather);
                                                tvwind.setText(wind);
                                            }else
                                                Toast.makeText(ma,"当前天气获取失败",Toast.LENGTH_SHORT).show();
                                        }else {
                                            //在此查看返回数据失败的原因
                                            Code code = weatherBean.getCode();
                                            Log.i("code", "failed code: " + code);
                                        }
                                    }
                                });
                        //获取未来7天的天气
                        QWeather.getWeather7D(ma, cityId, new QWeather.OnResultWeatherDailyListener() {
                            @Override
                            public void onError(Throwable throwable) {
                                Log.i("weather3D", "getWeather onError: ");
                            }

                            @Override
                            public void onSuccess(WeatherDailyBean weatherDailyBean) {
                                if (weatherDailyBean.getCode() == Code.OK) {
                                    List<WeatherDailyBean.DailyBean> dailyBeanList = weatherDailyBean.getDaily();
                                    WeatherDailyBean.DailyBean daily1Bean = dailyBeanList.get(0);
                                    WeatherDailyBean.DailyBean daily2Bean = dailyBeanList.get(1);
                                    WeatherDailyBean.DailyBean daily3Bean = dailyBeanList.get(2);
                                    WeatherDailyBean.DailyBean daily4Bean = dailyBeanList.get(3);
                                    WeatherDailyBean.DailyBean daily5Bean = dailyBeanList.get(4);
                                    WeatherDailyBean.DailyBean daily6Bean = dailyBeanList.get(5);
                                    WeatherDailyBean.DailyBean daily7Bean = dailyBeanList.get(6);

                                    String fx1Ddate = daily1Bean.getFxDate();
                                    String fx2Ddate = daily2Bean.getFxDate();
                                    String fx3Ddate = daily3Bean.getFxDate();
                                    String fx4Ddate = daily4Bean.getFxDate();
                                    String fx5Ddate = daily5Bean.getFxDate();
                                    String fx6Ddate = daily6Bean.getFxDate();
                                    String fx7Ddate = daily7Bean.getFxDate();

                                    String min1Dtemp = daily1Bean.getTempMin();
                                    String min2Dtemp = daily2Bean.getTempMin();
                                    String min3Dtemp = daily3Bean.getTempMin();
                                    String min4Dtemp = daily4Bean.getTempMin();
                                    String min5Dtemp = daily5Bean.getTempMin();
                                    String min6Dtemp = daily6Bean.getTempMin();
                                    String min7Dtemp = daily7Bean.getTempMin();

                                    String max1Dtemp = daily1Bean.getTempMax();
                                    String max2Dtemp = daily2Bean.getTempMax();
                                    String max3Dtemp = daily3Bean.getTempMax();
                                    String max4Dtemp = daily4Bean.getTempMax();
                                    String max5Dtemp = daily5Bean.getTempMax();
                                    String max6Dtemp = daily6Bean.getTempMax();
                                    String max7Dtemp = daily7Bean.getTempMax();

                                    String fx1Dwind = daily1Bean.getWindDirDay();
                                    String fx2Dwind = daily2Bean.getWindDirDay();
                                    String fx3Dwind = daily3Bean.getWindDirDay();
                                    String fx4Dwind = daily4Bean.getWindDirDay();
                                    String fx5Dwind = daily5Bean.getWindDirDay();
                                    String fx6Dwind = daily6Bean.getWindDirDay();
                                    String fx7Dwind = daily7Bean.getWindDirDay();

                                    String fx1Dtextday = daily1Bean.getTextDay();
                                    String fx2Dtextday = daily2Bean.getTextDay();
                                    String fx3Dtextday = daily3Bean.getTextDay();
                                    String fx4Dtextday = daily4Bean.getTextDay();
                                    String fx5Dtextday = daily5Bean.getTextDay();
                                    String fx6Dtextday = daily6Bean.getTextDay();
                                    String fx7Dtextday = daily7Bean.getTextDay();

                                    max1Dtemp = max1Dtemp + "℃";
                                    max2Dtemp = max2Dtemp + "℃";
                                    max3Dtemp = max3Dtemp + "℃";
                                    max4Dtemp = max4Dtemp + "℃";
                                    max5Dtemp = max5Dtemp + "℃";
                                    max6Dtemp = max6Dtemp + "℃";
                                    max7Dtemp = max7Dtemp + "℃";

                                    min1Dtemp = min1Dtemp + "℃";
                                    min2Dtemp = min2Dtemp + "℃";
                                    min3Dtemp = min3Dtemp + "℃";
                                    min4Dtemp = min4Dtemp + "℃";
                                    min5Dtemp = min5Dtemp + "℃";
                                    min6Dtemp = min6Dtemp + "℃";
                                    min7Dtemp = min7Dtemp + "℃";

                                    tv1Dtime.setText(fx1Ddate);
                                    tv2Dtime.setText(fx2Ddate);
                                    tv3Dtime.setText(fx3Ddate);
                                    tv4Dtime.setText(fx4Ddate);
                                    tv5Dtime.setText(fx5Ddate);
                                    tv6Dtime.setText(fx6Ddate);
                                    tv7Dtime.setText(fx7Ddate);

                                    tv1Dmintemp.setText(min1Dtemp);
                                    tv2Dmintemp.setText(min2Dtemp);
                                    tv3Dmintemp.setText(min3Dtemp);
                                    tv4Dmintemp.setText(min4Dtemp);
                                    tv5Dmintemp.setText(min5Dtemp);
                                    tv6Dmintemp.setText(min6Dtemp);
                                    tv7Dmintemp.setText(min7Dtemp);

                                    tv1Dmaxtemp.setText(max1Dtemp);
                                    tv2Dmaxtemp.setText(max2Dtemp);
                                    tv3Dmaxtemp.setText(max3Dtemp);
                                    tv4Dmaxtemp.setText(max4Dtemp);
                                    tv5Dmaxtemp.setText(max5Dtemp);
                                    tv6Dmaxtemp.setText(max6Dtemp);
                                    tv7Dmaxtemp.setText(max7Dtemp);

                                    tv1Dtextday.setText(fx1Dtextday);
                                    tv2Dtextday.setText(fx2Dtextday);
                                    tv3Dtextday.setText(fx3Dtextday);
                                    tv4Dtextday.setText(fx4Dtextday);
                                    tv5Dtextday.setText(fx5Dtextday);
                                    tv6Dtextday.setText(fx6Dtextday);
                                    tv7Dtextday.setText(fx7Dtextday);

                                    tv1Dwind.setText(fx1Dwind);
                                    tv2Dwind.setText(fx2Dwind);
                                    tv3Dwind.setText(fx3Dwind);
                                    tv4Dwind.setText(fx4Dwind);
                                    tv5Dwind.setText(fx5Dwind);
                                    tv6Dwind.setText(fx6Dwind);
                                    tv7Dwind.setText(fx7Dwind);

                                } else {
                                    //在此查看返回数据失败的原因
                                    Code code = weatherDailyBean.getCode();
                                    Log.i("code", "failed code: " + code);
                                }
                            }
                        });
                    }else{
                        Code code = geoBean.getCode();
                        Log.i("code", "failed code: " + code);
                    }
                }
            });

        }
    }

    private void initData(){
        if (user.getCounty() != null && !user.getCounty().equals("")){
            tvcity.setText(user.getCounty());
        }else
            tvcity.setText("未选择城市");
//        kmeansTimer();
        userTempMin = 20;
        userTempMax = 25;
        if (user.getMaxtmp() != null && !user.getMaxtmp().equals("")) {
            userTempMax = Double.parseDouble(user.getMaxtmp());
        }
        if (user.getMintmp() != null && !user.getMintmp().equals("")){
            userTempMin = Double.parseDouble(user.getMintmp());
        }

        //设置温度聚类中心
        double systemTempCenter1,systemTempCenter2,systemTempCenter3;
        double systemHumCenter1,systemHumCenter2,systemHumCenter3;
        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter= new SimpleDateFormat("MM");   //获取月份
        Date date = new Date(System.currentTimeMillis());
        String nowTime = formatter.format(date);
        isOld = user.getHaveold();
        isDisease = user.getHavedisease();
        if (user.getHaveold() == null || user.getHaveold().equals(""))
            isOld = "否";
        else
            isOld = user.getHaveold();
        if (user.getHavedisease() == null || user.getHavedisease().equals("") )
            isDisease = "否";
        else
            isDisease = user.getHavedisease();
        int Mouth = Integer.parseInt(nowTime);
        //设置系统推荐的温湿度聚类中心
        if(isOld.equals("是")){
            systemHumMin = 45.0;
            systemHumMax = 50.0;
            if (Mouth>=5 && Mouth<=8){  //夏季系统温湿度范围
                systemTempMax = 28.0;
                systemTempMin = 23.0;
            }else if (Mouth == 1 || Mouth >= 11){   //冬季温湿度范围
                systemTempMax = 25.0;
                systemTempMin = 18.0;
            }else {     //春秋季温湿度度范围
                systemTempMax = 25.0;
                systemTempMin = 20.0;
            }
        }else{
            if (isDisease.equals("是")){
                systemHumMin = 40.0;
                systemHumMax = 50.0;
                if (Mouth>=5 && Mouth<=8){  //夏季系统温湿度范围
                    systemTempMax = 28.0;
                    systemTempMin = 23.0;
                }else if (Mouth == 1 || Mouth >= 11){   //冬季温湿度范围
                    systemTempMax = 25.0;
                    systemTempMin = 18.0;
                }else {     //春秋季温湿度度范围
                    systemTempMax = 25.0;
                    systemTempMin = 20.0;
                }
            }else {
                if (Mouth>=5 && Mouth<=8){  //夏季系统温湿度范围
                    systemTempMax = 28.0;
                    systemTempMin = 23.0;
                    systemHumMin = 40.0;
                    systemHumMax = 80.0;
                }else if (Mouth == 1 || Mouth >= 11){   //冬季温湿度范围
                    systemTempMax = 25.0;
                    systemTempMin = 18.0;
                    systemHumMin = 30.0;
                    systemHumMax = 60.0;
                }else {     //春秋季温湿度度范围
                    systemTempMax = 25.0;
                    systemTempMin = 20.0;
                    systemHumMin = 35.0;
                    systemHumMax = 70.0;
                }
            }
        }
        systemTempCenter1 = (systemTempMin + systemTempMax) / 2;
        systemTempCenter2 = 2 * systemTempMin - systemTempCenter1 - 0.1;
        systemTempCenter3 = 2 * systemTempMax  - systemTempCenter1 + 0.1;
        systemHumCenter1 = (systemHumMin + systemHumMax) / 2;
        systemHumCenter2 = 2 * systemHumMin - systemHumCenter1 - 0.1;
        systemHumCenter3 = 2 * systemHumMax - systemHumCenter1 + 0.1;
        systemTempCenter = new double[]{systemTempCenter1, systemTempCenter2,systemTempCenter3};
        systemHumCenter = new double[]{systemHumCenter1, systemHumCenter2,systemHumCenter3};
        System.out.println(systemTempCenter[0] + " " + systemTempCenter[1]  + " " + systemTempCenter[2]);
        System.out.println(systemHumCenter[0] + " " + systemHumCenter[1]);

        //设置温度聚类中心
        double c1, c2, c3;
        c1 = (userTempMax + userTempMin) / 2;
        c2 = c1 - userTempMin + 0.1 + userTempMax;
        c3 = userTempMin - (c1 - userTempMin + 0.1);
        tempCenter = new double[]{c1, c2, c3};
        System.out.println(tempCenter[0] + " " + tempCenter[1] + " " + tempCenter[2]);
    }


    private void initListener(){
        citychange.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.ic_city_change) {
                ma.gotoNextActivity(CityActivity.class,user);
            }
        }
    };
    
    private void initKmeans(){
        ScheduledExecutorService service = Executors
                .newSingleThreadScheduledExecutor();
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        // 10：秒   5：秒
        // 第一次执行的时间为10秒，然后每隔五秒执行一次
        service.scheduleAtFixedRate(getHourValues, 1, 3600, TimeUnit.SECONDS);
//        service.scheduleAtFixedRate(valuableKmeans,5,3600,TimeUnit.SECONDS);

    }

    Runnable getHourValues = new Runnable() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void run() {
            //获取当前小时的数据
            @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");   //2015-01-10T08:00:35
            Date date = new Date(System.currentTimeMillis());
            String startTime = formatter.format(date.getTime() - 3600000);
            String endTime = formatter.format(date);
            System.out.println("date:" + date);
            tempHourValues = oneNetUtils.getDoubleInfoByTimer(Contests.WDStream,startTime,endTime);
            humHourValues = oneNetUtils.getDoubleInfoByTimer(Contests.SDStream,startTime,endTime);
            System.out.println( "tempHourValues:" + tempHourValues);
            if (tempHourValues != null || humHourValues != null){
                tempHourKmeansvalue = BasicKMeans.group(tempHourValues.toArray(new Double[tempHourValues.size()]), tempCenter);
                humHourKmeansValue = BasicKMeans.group(humHourValues.toArray(new Double[humHourValues.size()]), systemHumCenter);
                String humResult = judgeHum(BasicKMeans.center(findMaxSize(humHourKmeansValue)));
                String tempResult = judgeTemp(BasicKMeans.center(findMaxSize(tempHourKmeansvalue)));
                String result = humResult + "\n" + tempResult;
                tvjueche.setText(result);
            }else{
                tempHourKmeansvalue = null;
                humHourKmeansValue = null;
                tvjueche.setText("当前时段无数据");
            }
        }
    };

//    Runnable valuableKmeans = new Runnable(){
//        @Override
//        public void run() {
//            String humResult = judgeHum(BasicKMeans.center(findMaxSize(humHourKmeansValue)));
//            String tempResult = judgeTemp(BasicKMeans.center(findMaxSize(tempHourKmeansvalue)));
//            String result = humResult + "," + tempResult;
//            if (humResult.equals("")&&tempResult.equals("")){
//
//            }else
//                tvjueche.setText(result);
//        }
//    };

    private double[] findMaxSize(double[][] value){
        double[] maxDouble = null;
        int maxSize = 0;
        for (int i = 0; i < 3; i++) {
            if (value[i].length > maxSize){
                maxSize = value[i].length;
                maxDouble = value[i];
            }
        }
        return maxDouble;
    }
    private String judgeHum(double center){
        String humValue1 = "";
        String humValue2 = "";
        String humValue3 = "";
        String finValue = null;
        System.out.println("systemHumMin:" + systemHumMin + ",systemHumMax:" + systemHumMax);
        if(center>= systemHumMin && center <= systemHumMax){
            humValue1 += "当前湿度适宜";
        }else if (center > systemHumMax){
            humValue1 += "当前湿度超标";
            humValue3 += "，请开始除湿";
            if (isOld.equals("是")){
                humValue2 += "，可能影响老人与婴儿";
            }
            if (isDisease.equals("是")){
                humValue2 += "，可能引起呼吸疾病";
            }
        }else{
            humValue1 += "当前湿度过低";
            humValue3 += "，请开始加湿";
            if (isOld.equals("是")){
                humValue2 += "，可能影响老人与婴儿";
            }
            if (isDisease.equals("是")){
                humValue2 += "，可能引起呼吸疾病";
            }
        }
        finValue = humValue1 + humValue2 + humValue3;
        return finValue;
    }
    private String judgeTemp(double center){
        String tempStart = "";
        String tempConnect = "";
        String tempEnd = "";
        String tempValue = null;
        System.out.println("systemTempMax:" + systemTempMax + ",systemTempMin:" + systemTempMin);
        System.out.println("userTempMax:" + userTempMax + ",userTempMin:" + userTempMin);
        if (systemTempMax >= userTempMax && systemTempMin <= userTempMin){
            if(center < systemTempMin){
                tempStart += "当前温度过低";
            }else if (center>= systemTempMin && center < userTempMin){
                tempStart += "当前温度较低";
            }else if (center>= userTempMin && center <= userTempMax){
                tempStart += "当前温度适宜";
            }else if (center> userTempMax && center <= systemTempMax){
                tempStart += "当前温度较高";
            }else if (center > systemTempMax){
                tempStart += "当前温度过高";
            }
        }
        else if (systemTempMax < userTempMax && systemTempMin <= userTempMin){
            if(center < systemTempMin){
                tempStart += "当前温度过低";
                tempEnd += "已开始自动关窗";
            }else if (center>= systemTempMin && center < userTempMin){
                tempStart += "当前温度较低";
                tempEnd += "，建议关窗";
            }else if (center>= userTempMin && center <= systemTempMax){
                tempStart += "当前温度适宜";
            }else if (center> systemTempMax && center <= userTempMax){
                tempStart += "当前温度符合用户喜好";
                tempConnect += "，但较高于本季适宜温度";
                tempEnd += "，建议开窗";
            }else if (center > userTempMax){
                tempStart += "当前温度过高";
                tempEnd += "已开始自动开窗";
            }

        }
        else if (systemTempMax >= userTempMax && systemTempMin > userTempMin) {
            if (center < userTempMin) {
                tempStart += "当前温度过低";
                tempEnd += "已开始自动关窗";
            } else if (center >= userTempMin && center < systemTempMin) {
                tempStart += "当前温度符合用户喜好";
                tempConnect += "，但较低于于本季适宜温度";
                tempEnd += "，建议关窗";
            } else if (center >= systemTempMin && center <= userTempMax) {
                tempStart += "当前温度适宜";
            } else if (center > userTempMax && center <= systemTempMax) {
                tempStart += "当前温度较高";
                tempEnd += "，建议开窗";
            } else if (center>systemTempMax){
                tempStart += "当前温度过高";
                tempEnd += "已开始自动开窗";
            }
        }
        else if (systemTempMax < userTempMax && systemTempMin > userTempMin) {
            if (center < userTempMin) {
                tempStart += "当前温度过低";
                tempEnd += "已开始自动关窗";
            } else if (center >= userTempMin && center < systemTempMin) {
                tempStart += "当前温度符合用户喜好";
                tempConnect += "，但较低于本季适宜温度";
                tempEnd += "，建议关窗";
            } else if (center >= systemTempMin && center <= systemTempMax) {
                tempStart += "当前温度适宜";
            } else if (center > systemTempMax && center <= userTempMax) {
                tempStart += "当前温度符合用户喜好";
                tempConnect += "，但较高于本季适宜温度";
                tempEnd += "，建议开窗";
            } else if (center > userTempMax){
                tempStart += "当前温度过高";
                tempEnd += "已开始自动开窗";
            }
        }
        else if (systemTempMax <= userTempMin) {
            if (center < userTempMin && center>= systemTempMin) {
                tempStart += "当前温度适宜";
            } else if (center >= userTempMin && center <= userTempMax) {
                tempStart += "当前温度符合用户喜好";
                tempConnect += "，但较高于本季适宜温度";
                tempEnd += "，建议关窗";
            }else if (center > userTempMax) {
                tempStart += "当前温度过高";
                tempEnd += "已开始自动开窗";
            }else if (center < systemTempMin){
                tempStart += "当前温度过低";
                tempEnd += "已开始自动关窗";
            }
        }
        else if (systemTempMin > userTempMax) {
            if (center <= systemTempMax && center> userTempMax) {
                tempStart += "当前温度适宜";
            } else if (center >= userTempMin && center <= userTempMax) {
                tempStart += "当前温度符合用户喜好";
                tempConnect += "，但较低于本季适宜温度";
                tempEnd += "，建议关窗";
            }else if (center < userTempMin) {
                tempStart += "当前温度过低";
                tempEnd += "已开始自动关窗";
            }else if (center > systemTempMax){
                tempStart += "当前温度过高";
                tempEnd += "已开始自动开窗";
            }
        }
        tempValue = tempStart + tempConnect + tempEnd;
        return tempValue;
    }
    private void sendNotification(String text) {
        //获取NotificationManager实例
             notifyManager = (NotificationManager) ma.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel =new NotificationChannel("leo","测试通知",NotificationManager.IMPORTANCE_HIGH);
            notifyManager.createNotificationChannel(channel);
        }
        //实例化NotificationCompat.Builde并设置相关属性
        Notification notification = new NotificationCompat.Builder(ma,"leo")
                //设置小图标
                .setSmallIcon(R.drawable.image_boy)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.image_boy))
                //设置通知标题
                .setContentTitle(text)
                //设置通知内容
                .setContentText(text)
                .setAutoCancel(true)
                .build();
        //设置通知时间，默认为系统发出通知的时间，通常不用设置
        //.setWhen(System.currentTimeMillis());
        //通过builder.build()方法生成Notification对象,并发送通知,id=1
        notifyManager.notify(1, notification);
    }

}
